package edu.spsu.cs4253;


import java.net.*;
import java.io.*;

public class UDPClient {

	/**
	 *UDPClient will send a request and then receive data 
	 */
	public static void main(String[] args) throws Exception {
		 /**
		  *  Read message/data from keyboard or file 
		  *  Should ask for...
		  *  ...host name
		  *  ...port # of the receiver's socket
		  *  ...port # of this process's socket
		  *  ...a request message
		  */
		
		
		  String hostName = "localhost"; //Default host 
		  String servPort = "69";// Default  port number the TFTP port
		  //String request = ""; //The file the client request from the server
		  String temp = "";
		  int clientPort = 2121; //Default port for this process (client)
		  //SPSUSocket newSocket = null;
		  int serverPort;
		  
		   	//UDP part
		  /*
		  DatagramSocket clientSocket = null;
		  
		 
		  
		  InputStreamReader reader = new InputStreamReader(System.in);
		  BufferedReader br = new BufferedReader(reader);
		  
		  System.out.println("Enter the server host name/number (Leave Blank for Default): ");
		  temp = br. readLine();
		        if(temp.length() != 0) 
			 	    hostName = temp;
				
		  System.out.println("Enter the server port number: ");
		  temp = br.readLine();
			 	if (temp.length() != 0) 
			 		servPort = temp;
			 	
		  //System.out.println("Which file do you are you requesting?: ");
		  //request = br.readLine();
		
				
	     //try{

	    	 clientSocket = new DatagramSocket(clientPort);
	    	 clientSocket.setSoTimeout(500);
	    	 InetAddress receiverHost = InetAddress.getByName(hostName);
	    	 int receiverPort = Integer.parseInt(servPort);
	    	 byte[] sendData = new byte[1024];
	    	 byte[] receiveData = new byte[1024];
	    	 System.out.println("Send data:");
	    	 String requestData = br.readLine();
	    	 sendData = requestData.getBytes();
	    	DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, receiverHost, receiverPort);
	    	clientSocket.send(sendPacket);
	    	System.out.println("packet sent");
	    	DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
	    	clientSocket.receive(receivePacket);
	    	String serverReceipt = new String (receivePacket.getData());
	    	System.out.println("Server:" + serverReceipt);
	    	clientSocket.close();
	    	 //newSocket.sendMessage(receiverHost, receiverPort, request.getBytes()); 
		     //newSocket.close(); 
	    	 
	    // }
	     //catch(Exception ex){
	    	// ex.printStackTrace();
	     //}
	      
	      */
		  
		  //TCP 
	 
		  Socket clientSocket = null;
		  String message;
		  String modifiedMessage;
		  BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
		  
		  System.out.println("Enter the server host name/number (Leave Blank for Default): ");
		  temp = br. readLine();
		        if(temp.length() != 0) 
			 	    hostName = temp;
				
		  System.out.println("Enter the server port number: ");
		  temp = br.readLine();
			 	if (temp.length() != 0) 
			 		servPort = temp;
			 	
		  
		  serverPort = Integer.parseInt(servPort);
		  clientSocket = new Socket(hostName, serverPort);
		  DataOutputStream toServer = new DataOutputStream(clientSocket.getOutputStream());
		  BufferedReader fromServer = new BufferedReader (new InputStreamReader (clientSocket.getInputStream()));
		  message = fromServer.readLine();
		  toServer.writeBytes(message + '\n');
		  modifiedMessage = fromServer.readLine();
		  System.out.println("FROM SERVER:" + modifiedMessage);
		  clientSocket.close();
		
		
		 
		      
	}

}
